package view.panel
{
	import control.FamousPanelControl;
	import control.panel.FamusRemarkControl;
	import core.Control;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import global.GameState;
	
	import utils.TextTool;
	
	import view.components.LabelButton;

	public class FamusRemark extends Sprite
	{
	
		
		private var contents:Array;
		private var currentIndex:int;
		private var leftBut:LabelButton;
		private var rightBut:LabelButton;
		private var addBut:LabelButton;
		private var textArea:*;
		private var control:FamusRemarkControl;
		
		private static var FILTER_NAME:String = "$playerName";
		
		public function FamusRemark(control:FamusRemarkControl)
		{
			
			this.control = control;
			initTextArea();
			initButs();
			
		}
		
		private function initButs():void
		{
			leftBut = new LabelButton(textArea.x + textArea.width - 52,8,0,onBut,"panel_left");
			addChild(leftBut);
			
			addBut = new LabelButton(textArea.x + textArea.width - 36,8,1,onBut,"panel_add");
			addChild(addBut);
			
			rightBut = new LabelButton(textArea.x + textArea.width - 20,8,2,onBut,"panel_right");
			addChild(rightBut);
			
		}
		
		private function onBut(event:MouseEvent):void
		{
			switch (event.target.id)
			{
				case 0:
					back();
					break;
				case 1:
					Control.getInstance().famousPanelControl.open();
					break;
				case 2:
					next();
					break;
			}
		}
		
		private function initTextArea():void
		{
			textArea = TextTool.getTextArea(270,100);
			textArea.textField.mouseEnabled = false;
			addChild(textArea);
		}

		public function updateContent(contents:Array):void
		{
			this.contents = contents;
			currentIndex = 0;
		}
		
		public function next():void
		{
			currentIndex++;
			if (currentIndex >= contents.length)
			{
				currentIndex = 0;
			}
			textArea.htmlText = "<font color='#FFFFFF' size='12' >" + filter(contents[currentIndex]) + "</font>";
			
		}
		
		public function back():void
		{
			currentIndex--;
			if (currentIndex < 0)
			{
				currentIndex = contents.length - 1;
			}
			textArea.htmlText = "<font color='#FFFFFF' size='12' >" + filter(contents[currentIndex]) + "</font>";
		}
		
		private function filter(content:String):String
		{
			if (content.indexOf(FILTER_NAME)>= 0)
			{
				return content.replace(FILTER_NAME,GameState.playerName);
			}
			return content;
		}
	}
}